import { defineStore } from 'pinia'

const useNoticeStore = defineStore('notice', {
    state: () => {
        return {
            notice: {
                trans: {
                    //订单交易信息
                    key: 4,
                    title: '交易物流',
                    lastContent: '',
                    lastTime: '',
                    unread: 0
                },
                system: {
                    // 系统通知
                    key: 5,
                    title: '通知公告',
                    lastContent: '',
                    lastTime: '',
                    unread: 0
                },
                afterSales: {
                    // 售后消息
                    key: 6,
                    title: '退款售后',
                    lastContent: '',
                    lastTime: '',
                    unread: 0
                },
                settlementBill: {
                    key: 7,
                    title: '账单通知',
                    lastContent: '',
                    lastTime: '',
                    unread: 0
                }
            }
        }
    },
    actions: {
        transInit(userId: string | number) {
            // 查询最新通知
            uni.$u.$api.ImTrans.unReadList().then((ftrans: Array<any>) => {
                if (ftrans && ftrans.length > 0) {
                    let item = ftrans[0]
                    this.notice.trans.lastContent = item.title
                    this.notice.trans.unread = ftrans.length
                    this.notice.trans.lastTime = item.updateTime
                    // 将新数据存入本地
                    uni.$u.$imHandler.sqlite.imTrans.saveOrUpdateTrans(ftrans)
                    // 重置总未读记录数
                    // vm.$u.$imHandler.updateTabBarText()
                } else {
                    // 去本地最后一条数据
                    uni.$u.$imHandler.sqlite.imTrans
                        .getList({
                            whereObj: {
                                userId: userId
                            },
                            orderBy: 'order by update_time desc',
                            limit: 1
                        })
                        .then((trs: Array<any>) => {
                            if (trs && trs.length > 0) {
                                let item = trs[0]
                                this.notice.trans.lastContent = item.title
                                this.notice.trans.unread = 0
                                this.notice.trans.lastTime = item.updateTime
                                // 重置总未读记录数
                                // vm.$u.$imHandler.updateTabBarText()
                            }
                        })
                }
            })
        },
        systemInit() {
            // 取本地最后一条记录
            uni.$u.$imHandler.sqlite.imSystem
                .getList({
                    orderBy: 'order by update_time desc',
                    limit: 1
                })
                .then((trs: Array<any>) => {
                    let firstTime, lastTime, bLastSystem: any, size
                    if (trs && trs.length > 0) {
                        bLastSystem = trs[0]
                        lastTime = bLastSystem.updateTime
                    } else {
                        firstTime = uni.$u.$timeUtil.formatDate(new Date())
                        size = 20
                    }
                    // 查询最新数据
                    uni.$u.$api.ImSystem.historyList({ firstTime, lastTime, size }).then((fsysList: Array<any>) => {
                        let unreadCount = 0
                        if (fsysList && fsysList.length > 0) {
                            let item = fsysList[0]
                            this.notice.system.lastContent = item.title
                            this.notice.system.lastTime = item.updateTime
                            unreadCount = fsysList.length
                            // 将新数据存入本地
                            uni.$u.$imHandler.sqlite.imSystem.saveOrUpdateSystem(fsysList)
                        } else {
                            if (bLastSystem) {
                                this.notice.system.lastContent = bLastSystem.title
                                this.notice.system.lastTime = bLastSystem.updateTime
                            }
                        }
                        // 查询本地未读记录数
                        if (trs && trs.length > 0) {
                            uni.$u.$imHandler.sqlite.imSystem
                                .getCount({
                                    whereObj: {
                                        isRead: 0
                                    }
                                })
                                .then((countArr: Array<any>) => {
                                    if (countArr && countArr.length > 0) {
                                        this.notice.system.unread = countArr[0].count + unreadCount
                                    } else {
                                        this.notice.system.unread = unreadCount
                                    }
                                    // 重置总未读记录数
                                    // vm.$u.$imHandler.updateTabBarText()
                                })
                        } else {
                            this.notice.system.unread = unreadCount
                            // 重置总未读记录数
                            // vm.$u.$imHandler.updateTabBarText()
                        }
                    })
                })
        },
        afterSalesInit(userId: string | number) {
            // 查询最新通知
            uni.$u.$api.ImAfterSales.unReadList().then((fAfterSales: Array<any>) => {
                if (fAfterSales && fAfterSales.length > 0) {
                    let item = fAfterSales[0]
                    this.notice.afterSales.lastContent = item.title
                    this.notice.afterSales.unread = fAfterSales.length
                    this.notice.afterSales.lastTime = item.updateTime
                    // 将新数据存入本地
                    uni.$u.$imHandler.sqlite.imAfterSales.saveOrUpdateAfterSales(fAfterSales)
                    // 重置总未读记录数
                    // vm.$u.$imHandler.updateTabBarText()
                } else {
                    // 去本地最后一条数据
                    uni.$u.$imHandler.sqlite.imAfterSales
                        .getList({
                            whereObj: {
                                userId: userId
                            },
                            orderBy: 'order by update_time desc',
                            limit: 1
                        })
                        .then((trs: Array<any>) => {
                            if (trs && trs.length > 0) {
                                let item = trs[0]
                                this.notice.afterSales.lastContent = item.title
                                this.notice.afterSales.unread = 0
                                this.notice.afterSales.lastTime = item.updateTime
                                // 重置总未读记录数
                                // vm.$u.$imHandler.updateTabBarText()
                            }
                        })
                }
            })
        },
        settlementBillInit(userId: string) {
            // 查询最新通知
            uni.$u.$api.ImSettlementBill.unReadList().then((fSettlementBill: Array<any>) => {
                if (fSettlementBill && fSettlementBill.length > 0) {
                    let item = fSettlementBill[0]
                    this.notice.settlementBill.lastContent = item.title
                    this.notice.settlementBill.unread = fSettlementBill.length
                    this.notice.settlementBill.lastTime = item.updateTime
                    // 将新数据存入本地
                    uni.$u.$imHandler.sqlite.imSettlementBill.saveOrUpdateSettlementBill(fSettlementBill)
                    // 重置总未读记录数
                    // vm.$u.$imHandler.updateTabBarText()
                } else {
                    // 去本地最后一条数据
                    uni.$u.$imHandler.sqlite.imSettlementBill
                        .getList({
                            whereObj: {
                                userId: userId
                            },
                            orderBy: 'order by update_time desc',
                            limit: 1
                        })
                        .then((trs: Array<any>) => {
                            if (trs && trs.length > 0) {
                                let item = trs[0]
                                this.notice.settlementBill.lastContent = item.title
                                this.notice.settlementBill.unread = 0
                                this.notice.settlementBill.lastTime = item.updateTime
                                // 重置总未读记录数
                                // vm.$u.$imHandler.updateTabBarText()
                            }
                        })
                }
            })
        },
        updateTrans({ lastContent, lastTime, num }: { lastContent: string; lastTime: string; num: number }) {
            if (lastContent) this.notice.trans.lastContent = lastContent
            if (lastTime) this.notice.trans.lastTime = lastTime
            if (num) this.notice.trans.unread += num
        },
        clearTransUnread() {
            this.notice.trans.unread = 0
        },
        changeTransUnread(num: number) {
            this.notice.trans.unread += num
        },
        updateSystem({ lastContent, lastTime, num }: { lastContent: string; lastTime: string; num: number }) {
            if (lastContent) this.notice.system.lastContent = lastContent
            if (lastTime) this.notice.system.lastTime = lastTime
            if (num) this.notice.system.unread += num
        },
        clearSystemUnread() {
            this.notice.system.unread = 0
        },
        changeSystemUnread(num: number) {
            this.notice.system.unread += num
        },
        updateAfterSales({ lastContent, lastTime, num }: { lastContent: string; lastTime: string; num: number }) {
            if (lastContent) this.notice.afterSales.lastContent = lastContent
            if (lastTime) this.notice.afterSales.lastTime = lastTime
            if (num) this.notice.afterSales.unread += num
        },
        clearAfterSalesUnread() {
            this.notice.afterSales.unread = 0
        },
        changeAfterSalesUnread(num: number) {
            this.notice.afterSales.unread += num
        },
        updateSettlementBill({ lastContent, lastTime, num }: { lastContent: string; lastTime: string; num: number }) {
            if (lastContent) this.notice.settlementBill.lastContent = lastContent
            if (lastTime) this.notice.settlementBill.lastTime = lastTime
            if (num) this.notice.settlementBill.unread += num
        },
        clearSettlementBillUnread() {
            this.notice.settlementBill.unread = 0
        },
        changeSettlementBillUnread(num: number) {
            this.notice.settlementBill.unread += num
        },
        clearAllNoticeUnread() {
            this.notice.trans.unread = 0
            this.notice.system.unread = 0
            this.notice.afterSales.unread = 0
            this.notice.settlementBill.unread = 0
        }
    }
})

export default useNoticeStore
